package com.zros.modules.business.controller;

import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zros.common.api.vo.PageResult;
import com.zros.common.api.vo.Result;
import com.zros.common.exception.JeecgBootException;
import com.zros.modules.business.dto.CreateClueDto;
import com.zros.modules.business.dto.QueryClueDto;
import com.zros.modules.business.dto.UpdateClueDto;
import com.zros.modules.business.service.IClueService;
import com.zros.modules.business.vo.ClueInfoVo;
import com.zros.modules.shiro.authc.util.JwtUtil;
import com.zros.modules.system.dto.UserInfo;
import com.zros.modules.system.service.IUserService;

/**
 * <p>
 * 线索表 前端控制器
 * </p>
 *
 * @author pangyinqiang
 * @since 2019-04-16
 */
@RestController
@RequestMapping("/business/clue")
@PropertySource("classpath:config.properties")
public class ClueController {

	@Value("${authority.campusKey}")
	private String authorityCampusKey;
	
	@Value("${authority.companyKey}")
	private String authorityCompanyKey;
	
	@Value("${authority.selfKey}")
	private String authoritySelfKey;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IClueService clueService;

	/**
	 * 分页查询线索
	 */
	@ApiOperation(value = "分页查询线索")
	//@RequiresPermissions("business:clue:query")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public PageResult<ClueInfoVo> queryPageList(HttpServletRequest request, QueryClueDto queryClueDto, Page<ClueInfoVo> page) 
	{
		//获取当前用户
		UserInfo currentUser = new UserInfo();//queryCurrentUser(request,"queryClueOrStatistic");
		//获取数据权限key
		String dataResourceKey ="300";// currentUser.getDataResourceKey();
		currentUser.setId("1130485303195303938");
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		if(authorityCampusKey.equals(dataResourceKey))
		{
			queryClueDto.setUserId(currentUser.getId());
			queryClueDto.setCampusKey("随便塞不为空");
		}
		else if(authoritySelfKey.equals(dataResourceKey))
		{
			queryClueDto.setUserId(currentUser.getId());
			queryClueDto.setSelfKey("随便塞不为空");
		}
		else if(authorityCompanyKey.equals(dataResourceKey))
		{
			//gogogo
		}
		else
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		Page<ClueInfoVo> pageClueInfo = clueService.selectClueList(page, queryClueDto);
        PageResult<ClueInfoVo> pageResult = new PageResult<ClueInfoVo>(pageClueInfo.getTotal(), pageClueInfo.getRecords());
        return pageResult;
	}
	
	/**
	 * 根据id查询线索
	 */
	@ApiOperation(value = "根据id查询线索")
	@RequiresPermissions("business:clue:query")
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public Result<ClueInfoVo> query(Long clueId) {
		Result<ClueInfoVo> result = new Result<ClueInfoVo>();
		result.success("查询成功").setData(clueService.info(clueId));
		return result;
	}
	
	/**
	 * 添加线索
	 */
	@ApiOperation(value = "添加线索")
	@RequiresPermissions("business:clue:create")
	@PostMapping("/create")
	public Result<?> create(@RequestBody CreateClueDto createClueDto) {
		return new Result<>().success(clueService.createClue(createClueDto) ? "添加成功" : "添加失败，请重试");
	}

	/**
	 * 修改线索
	 */
	@ApiOperation(value = "修改线索")
	@RequiresPermissions("business:clue:update")
	@PostMapping("/update")
	public Result<?> update(@RequestBody UpdateClueDto updateClueDto) {
		return new Result<>().success(clueService.updateClue(updateClueDto) ? "修改成功" : "修改失败，请重试");
	}

	/**
	 * 删除线索
	 */
	@ApiOperation(value = "删除线索")
	@RequiresPermissions("business:clue:delete")
	@PostMapping("/delete/{id}")
	public Result<?> delete(@PathVariable("id") Long id) {
		if (null == id) {
			return new Result<>().error("ID不能为空");
		}
		return new Result<>().success(clueService.removeClue(id) ? "删除成功" : "删除失败，请重试");
	}
	
	/**
	 *  获取当前用户
	 */
	UserInfo queryCurrentUser(HttpServletRequest request,String queryMethod)
	{
		UserInfo currentUser = new UserInfo();
		String userAccount = JwtUtil.getUserAccountByToken(request);
		currentUser = userService.queryUserInfoByAccount(userAccount,queryMethod);
		
		return currentUser;
	}
	
	/**
	 * 导出线索
	 */
	@ApiOperation(value = "导出线索")
	@RequiresPermissions("business:export")
	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public PageResult<ClueInfoVo> exportPageList(HttpServletRequest request, QueryClueDto queryClueDto, Page<ClueInfoVo> page) 
	{
		//目前设置导出 分页为最大
		page.setSize(999999);
		
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request,"queryClueOrStatistic");
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		if(authorityCampusKey.equals(dataResourceKey))
		{
			queryClueDto.setUserId(currentUser.getId());
			queryClueDto.setCampusKey("随便塞不为空");
		}
		else if(authoritySelfKey.equals(dataResourceKey))
		{
			queryClueDto.setUserId(currentUser.getId());
			queryClueDto.setSelfKey("随便塞不为空");
		}
		else if(authorityCompanyKey.equals(dataResourceKey))
		{
			//gogogo
		}
		else
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		Page<ClueInfoVo> pageClueInfo = clueService.selectClueList(page, queryClueDto);
        PageResult<ClueInfoVo> pageResult = new PageResult<ClueInfoVo>(pageClueInfo.getRecords().size(), pageClueInfo.getRecords());
        return pageResult;
	}
	
	/**
	 * 通知列表查询
	 */
	@ApiOperation(value = "通知列表查询")
	@RequiresPermissions("business:clue:query")
	@RequestMapping(value = "/notifyList", method = RequestMethod.GET)
	public PageResult<ClueInfoVo> queryNotifyPageList(HttpServletRequest request, QueryClueDto queryClueDto, Page<ClueInfoVo> page) 
	{
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request,"queryClueOrStatistic");
		/*//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		if(authorityCampusKey.equals(dataResourceKey))
		{
			queryClueDto.setUserId(currentUser.getId());
			queryClueDto.setCampusKey("随便塞不为空");
		}
		else if(authoritySelfKey.equals(dataResourceKey))
		{
			queryClueDto.setUserId(currentUser.getId());
			queryClueDto.setSelfKey("随便塞不为空");
		}
		else if(authorityCompanyKey.equals(dataResourceKey))
		{
			//gogogo
		}
		else
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}*/
		
		queryClueDto.setUserId(currentUser.getId());
		Page<ClueInfoVo> pageClueInfo = clueService.selectNotifyClueList(page, queryClueDto);
        PageResult<ClueInfoVo> pageResult = new PageResult<ClueInfoVo>(pageClueInfo.getRecords().size(), pageClueInfo.getRecords());
        return pageResult;
	}
}
